<template>
	<view>
		<!-- 使用自定义的搜索组件 -->
		<my-search @my-click="gotosearch"  />
		
		<view class="scrolld-view-container">
			<!-- 左侧的滑动区域 -->
		<scroll-view scroll-y="true"  :style="{height:winheight + 'px'}"  class="left-scroll-view">
			<block v-for="(item,i) in cateList" :key="i">
				<view :class="['left-scroll-view-item',i === active ? 'active' : '']" @click="activeChanged(i)">
					{{item.cat_name}}
				</view>		
			</block>	
		</scroll-view>
			<!-- 右侧的滑动区域 -->
			<scroll-view scroll-y="true"  :style="{height:winheight + 'px'}" :scroll-top="scrollTop">
				<view class="cate-lv2" v-for="(item2,i2) in catelevel2" :key="i2"> 
				<!-- 二级分类列表的标题 -->
				<view class="cate-lv2-title" >
					/{{item2.cat_name}}/
				</view>
				<!-- 三级分类对应的列表 -->
				<view class="cate-lv3-list">
					<!-- 三级分类的item项 -->
					<view class="cate-lv3-item" v-for="(item3,i3) in item2.children" :key="i3" @click="gotoGoodsList(item3)">
						<!-- 三级分类下的内容与图片 -->
						<image :src="item3.cat_icon" ></image>
						<text>{{item3.cat_name}}</text>
					</view>
				</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
import badegeMix from '@/minxins/tabbar-badge.js'
	
	export default {
		mixins:[ badegeMix ],
				data() {
			return {
				winheight:0,
				//当前设备可用的高度
				cateList:[],
				//默认的激活项目
				active:0,
				//二级分类的列表
				catelevel2:[],
				scrollTop:0
			};
		},
		onLoad() {
			//一加载就调用这个api 用于获取屏幕高度
	const sysinfo =	uni.getSystemInfoSync()
	// console.log(sysinfo) 可以看到windowheight
	this.winheight =sysinfo.windowHeight - 60
	//调用获取左侧数据列表的方法·
	this.getCateList()
		},
		methods:{
		async getCateList(){
			const {data:res} = await uni.$http.get('/api/public/v1/categories')
			if(res.meta.status !== 200)return uni.$showMsg()
			this.cateList=res.message
			
			//为二级分类列表赋值
			this.catelevel2 = res.message[0].children
		},
		//动态绑定变化事件
		activeChanged(i){
			this.active = i,
			//动态绑定二级分类列表赋值
			this.catelevel2 = this.cateList[i].children
			
		this.scrollTop = this.scrollTop === 0 ? 1 : 0
		},
		//点击三级分类跳转到商品列表页面
		gotoGoodsList(item3){
			uni.navigateTo({
			url:'../../subpkg/goods_list/goods_list?cid=' + item3.cat_id	
			})
		},
		// 跳转到真正的搜索页面
		gotosearch(){
			uni.navigateTo({
				url:'/subpkg/search/search'
			})
		}
		},
		
	}
</script>

<style lang="scss">
.scrolld-view-container{
	display: flex;
	.left-scroll-view{
		width: 240rpx;
		
		.left-scroll-view-item{
			background-color: #FFFFFF;
			line-height: 90rpx;
			text-align: center;
font-size: 24rpx;

&.active{
	background-color: #f7f7f7;
	position: relative;
	color:#26a886;
	
	&::before {
		content: '';
		display: block;
   width: 6rpx;
	height: 30rpx;
	background-color: #19e1af;
	position: absolute;
	top: 50%;
	left: 0;
	transform: translateY(-50%);
	}
}
		}
	}
}

.cate-lv2-title{
	font-size: 30rpx;
	font-weight: 800;
	text-align: center;
	padding: 30rpx 0;
	color:#26a886;
}

.cate-lv3-list{
	display: flex;
	// 自动换行
	flex-wrap: wrap;
	.cate-lv3-item{
		width: 33%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-bottom: 10px;
		image {
			width: 120rpx;
			height: 120rpx;
		}
		text {
			font-size: 25rpx;
			color: #26a886;
		}
	}
}
</style>
